package com.bird.pro.ctrl;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bird.pro.pojo.BirdAdmin;
import com.bird.pro.pojo.BirdMenu;
import com.bird.pro.service.IAdminService;
import com.bird.pro.service.IMenuService;
import com.bird.pro.utils.Md5;

@Controller
@RequestMapping("/login")
public class LoginController {

	@Autowired
	private IAdminService adminService;
	@Autowired
	private IMenuService menuService;
	
	@RequestMapping("/login")
	public String login(String adminAccount, String adminPassword, HttpSession session, Model model){
		
		if (adminAccount == null && "".equals(adminAccount)) {
			return "/index";
		}
		List<BirdAdmin> admins = adminService.queryAdmins("", "", adminAccount);
		if (!admins.isEmpty()) {
			BirdAdmin admin = admins.get(0);
			String pass = Md5.getMD5(adminPassword);
			if (admin.getAdminPassword().equals(pass)) {
				session.setAttribute("admin", admin);
				List<BirdMenu> menus = menuService.queryMenus(admin.getId());
				session.setAttribute("menus", menus);
				return "/bird";
			}
		}
		return "/index";
	}
}
